<?php

class Application_Model_Dao_YearsStudent extends Zend_Db_Table_Abstract
{

	protected $_name = 'years_students';
    protected $_primary = 'id';
    	
	public function init(){
    	
    }
	public function getAllYear(){
    	$dbAdapter = Zend_Db_Table::getDefaultAdapter();
    	$select = $dbAdapter->select()->from('years_students')->joinLeft('faculties', 'years_students.faculty_id = faculties.id', array('faculty_designation' => 'designation')) 
    												 ->joinLeft('cathedrals', 'years_students.cathedral_id = cathedrals.id', array('cathedral_designation' => 'designation'))
    												 ->joinLeft('studies_type', 'years_students.studies_type_id = studies_type.id', array('studies_type_designation' => 'short'))
    												 ->joinLeft('specialities', 'years_students.speciality_id = specialities.id', array('specialities_designation' => 'designation'))
    												 ->joinLeft('users', 'years_students.tutor_id = users.id', array('users_name' => 'name','users_surname' => 'surname'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
    }
public function getRowYearsStudent($id){
		$select = $dbAdapter->select()->from('years_students')->joinLeft('faculties', 'years_students.faculty_id = faculties.id', array('faculty_designation' => 'designation')) 
    												 ->joinLeft('cathedrals', 'years_students.cathedral_id = cathedrals.id', array('cathedral_designation' => 'designation'))
    												 ->joinLeft('studies_type', 'years_students.studies_type_id = studies_type.id', array('studies_type_designation' => 'short'))
    												 ->joinLeft('specialities', 'years_students.speciality_id = specialities.id', array('specialities_designation' => 'designation'))
    												 ->joinLeft('users', 'years_students.tutor_id = users.id', array('users_name' => 'name','users_surname' => 'surname'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
 	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
			throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");	
		return $result;
    } 
	public function getYearsStudentByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
			throw new Exception("Rok o ID $id nie istnieje.");
		return $row->toArray();
	}


	public function saveYearsStudent($yearsStudent) {
    	$data = array(
    		'short_name' => $yearsStudent->getShortName(),
			'faculty_id' => $yearsStudent->getFaculty(),
    		'cathedral_id' => $yearsStudent->getCathedral(),
			'studies_type_id' => $yearsStudent->getStudiesType(),
    		'tutor_id' => $yearsStudent->getTutor(),
    		'number_students' => $yearsStudent->getNumberStudents(),
    		'speciality_id' => $yearsStudent->getSpeciality(),
    		'number_speciality' => $yearsStudent->getNumberSpeciality(),
    		'duration_semesters' => $yearsStudent->getDurationSemesters(),
		);
		$result = $this->insert($data);
     return $result;
    }
public function updateYearsStudent($yearsStudent) {
		$data = array(
    		'short_name' => $yearsStudent->getShortName(),
			'faculty_id' => $yearsStudent->getFaculty(),
    		'cathedral_id' => $yearsStudent->getCathedral(),
			'studies_type_id' => $yearsStudent->getStudiesType(),
    		'tutor_id' => $yearsStudent->getTutor(),
    		'number_students' => $yearsStudent->getNumberStudents(),
    		'speciality_id' => $yearsStudent->getSpeciality(),
    		'number_speciality' => $yearsStudent->getNumberSpeciality(),
    		'duration_semesters' => $yearsStudent->getDurationSemesters(),
		);
		$result = $this->update($data, 'id = '. (int)$yearsStudent->getId());
		return $result;
		}
		
public function deleteYearsStudent($yearsStudent){
		$result = $this->delete('id =' . (int)$yearsStudent->getId());
		return $result;
	}
		
public function getToSelect(){
    	//$array = $this->getAll()->toArray();
    	$studiesDao = new Application_Model_Dao_Studies();
  		$degreeStudies = $studiesDao->getToSelect();
    	$years[''] = "wybierz";
    	//foreach ($array as $option) {
     	//$years[ $option['id'] ] = $option['degreeStudy_id' ] ;
     //	}
     return $degreeStudies;
    }
    
	public function getToSelectInformationAboutYear() {	
		try {	
			$db = Zend_Db_Table::getDefaultAdapter();
			$sql = "SELECT y.id, y.short_name as year_name, st.short as studies_type, s.designation as speciality 
				FROM years_students as y
				INNER JOIN specialities as s
				JOIN studies_type as st";
    		$stmt = $db->query($sql);
		
		
    	$years[''] = "wybierz";
    	
    	while ($row = $stmt->fetch()) {    		
     		$years[ $row['id'] ] = $row['year_name']." : ".$row['studies_type']." , spec. ".$row['speciality']; 
     	}
     	
     	return $years;
		
		} catch (Exception $exception) {
				Zend_Registry::get('logger')->log('Brak wyników. Exception: '.$exception, Zend_Log::NOTICE);
		}
		
	}
}